window.onload = function () {
    var ua = navigator.userAgent;  //用于 HTTP 请求的用户代理头的值,获取客户端型号、浏览器型号方式
    if (ua.indexOf("com.tuhuan.health") !== -1 || ua.indexOf("com.tuhuanjk.thmember") !== -1) {
        var downappform = document.getElementById("downappform");
        if (downappform) {
            downappform.classList.add("hide");
        }
    }
}

document.addEventListener("DOMContentLoaded", function() {
    var navbarToggle = document.querySelector(".navbar-toggle");
    var bgDiv = document.querySelector(".bg-div");
    var body = document.querySelector("body"); // querySelector 选择第一个样式；ALL选择所有
    var navbarFormLinks = document.querySelectorAll(".navbar-form a");
    var navbarCollapse = document.querySelector(".navbar-collapse");
    var modalContent = document.querySelector(".modal-content");
    var modelDownButtons = document.querySelectorAll(".model-down, .model-down-1");
    var weixinLogoIcon = document.querySelector(".weixin-logo-icon");
    var shoujiLogoIcon = document.querySelector(".shouji-logo-icon");
    var navAgent = navigator.userAgent.toLowerCase(); // 将字符串转换为小写

    navbarToggle.addEventListener("click", function () {
        this.classList.toggle("click"); // 在元素中切换类名，第一个参数为要在元素中移除的类名，并返回 false，如果该类名不存在则会在元素中添加类名，并返回 true
        bgDiv.classList.toggle("hide");
        body.classList.toggle("body-static");
        navbarFormLinks.forEach(function(link) {  // 调用数组的每个元素，并将元素传递给回调函数
            link.classList.toggle("active");
        });
        navbarCollapse.style.display = (navbarCollapse.style.display === "none") ? "block" : "none";
    });

    bgDiv.addEventListener("click", function () { // 向指定元素添加事件
        navbarToggle.click();
    });

    var top = (window.innerHeight - modalContent.offsetHeight) / 2;
    modelDownButtons.forEach(function(button) {
        button.addEventListener("click", function () {
            modalContent.style.position = 'absolute';
            modalContent.style.top = top + 'px';
        });
    });

    weixinLogoIcon.addEventListener("mouseleave", function () {
        this.previousElementSibling.querySelector(".popover").classList.remove("hover");
    });

    weixinLogoIcon.addEventListener("mouseenter", function () {
        this.previousElementSibling.querySelector(".popover").classList.add("hover");
    });

    shoujiLogoIcon.addEventListener("mouseleave", function () {
        this.previousElementSibling.querySelector(".popover").classList.remove("hover");
    });

    shoujiLogoIcon.addEventListener("mouseenter", function () {
        this.previousElementSibling.querySelector(".popover").classList.add("hover");
    });

    if (navAgent.indexOf("thjk-app-ios") > -1 || navAgent.indexOf("thdoctor-app-ios") > -1 || navAgent.indexOf("thjk-app-android") > -1 || navAgent.indexOf("thdoctor-app-android") > -1) {
        document.querySelector(".navbar-form.navbar-right").style.display = "none";
        document.querySelector(".nav > li:last-child a").style.borderBottom = "0";
    }

    var clickLinks = document.querySelectorAll(".navbar-nav > li.click a");
    clickLinks.forEach(function(link) {
        link.removeAttribute("href");
    });
});

document.addEventListener("DOMContentLoaded", function () {
    function Accordion(el, multiple) {
        this.el = el || {};
        this.multiple = multiple || false;

        var links = this.el.querySelectorAll('.link');

        links.forEach(function (link) {
            link.addEventListener('click', function (e) {
                e.preventDefault(); // 阻止元素发生默认的行为
                next = this.nextElementSibling;
                next.style.display = (next.style.display === 'block') ? 'none' : 'block';
                this.parentNode.classList.toggle('open');
                if (!multiple) {
                    var submenus = el.querySelectorAll('.submenu');
                    submenus.forEach(function (submenu) {
                        if (submenu !== next) {
                            submenu.style.display = 'none';
                            submenu.parentNode.classList.remove('open');
                        }
                    });
                }
            });
        });

    }

    var accordion = new Accordion(document.getElementById('accordion'), false);

    var itemli = document.querySelectorAll("#accordion li");

    itemli.forEach(function (li) {
        li.addEventListener("mouseenter", function () {
            this.classList.add("hover");
            var siblings = getSiblings(this);
            siblings.forEach(function (sibling) {
                sibling.classList.remove("hover");
            });
        });

        li.addEventListener("mouseleave", function () {
            this.classList.remove("hover");
        });
    });

    function getSiblings(elem) {
        var siblings = [];
        var sibling = elem.parentNode.firstChild;
        while (sibling) {
            if (sibling.nodeType === 1 && sibling !== elem) {
                siblings.push(sibling);
            }
            sibling = sibling.nextSibling;
        }
        return siblings;
    }
});